package cn.itsource.service.impl;


import cn.itsource.bean.OrderPetAcquisition;
import cn.itsource.bean.SearchMasterMsg;
import cn.itsource.mapper.OrderPetAcquisitionMapper;
import cn.itsource.query.OrderPetAcquisitionQuery;
import cn.itsource.service.IOrderPetAcquisitionService;
import cn.itsource.service.ISearchMasterMsgService;
import cn.itsource.util.JsonResult;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

import static cn.itsource.util.PetHomeConstant.ORDER_STATE.ORDER_STATE_PENDING;
import static cn.itsource.util.PetHomeConstant.SEARCH_MSG.SEARCH_MSG_SURE;

@Service
@Slf4j
//Propagation.SUPPORTS:支持别人事务
@Transactional(readOnly = true, propagation = Propagation.SUPPORTS)
public class OrderPetAcquisitionServiceImpl extends ServiceImpl<OrderPetAcquisitionMapper,OrderPetAcquisition> implements IOrderPetAcquisitionService {

    @Resource
    private ISearchMasterMsgService searchMasterMsgService;


    @Override
    public JsonResult getPage(OrderPetAcquisitionQuery query) {
        Page<OrderPetAcquisition> page = new Page<>();
        LambdaQueryWrapper<OrderPetAcquisition> queryWrapper = new LambdaQueryWrapper<>();



        Page<OrderPetAcquisition> messagePage = this.page(page, queryWrapper);
        List<OrderPetAcquisition> records = messagePage.getRecords();


        Long total = messagePage.getTotal();



        return JsonResult.success();
    }

    @Override
    @Transactional
    public JsonResult sureOrder(OrderPetAcquisition order) {

        order.setState(ORDER_STATE_PENDING);
        SearchMasterMsg msg = searchMasterMsgService.getById(order.getMsgId());
        //设置接单状态
        msg.setState(SEARCH_MSG_SURE);


        searchMasterMsgService.updateById(msg);
        this.save(order);
        return JsonResult.success("订单接收成功");
    }
}
